//引入gulp
const gulp = require("gulp");

//引入解析组件
const sass = require("gulp-sass");
const autoprefixer = require("gulp-autoprefixer");
const cleancss = require("gulp-clean-css");
const rename = require("gulp-rename");
const uglify = require("gulp-uglify");
const del = require("del");
const watch = require("gulp-watch");
const browserSync = require("browser-sync").create();
const reload = browserSync.reload;
const plumber = require("gulp-plumber");
const connect=require("gulp-connect");
// const proxy = require("http-proxy-middleware");
var browserify = require("browserify");
var babelify = require("babelify");
var source = require("vinyl-source-stream");
var buffer = require("vinyl-buffer");
var es = require("event-stream");

gulp.task("sass", (done) => {
	gulp
		.src("./src/sass/*.{scss,sass}")
		.pipe(sass()) // 通过sass插件将sass编译为css，如果需要编译less，则改用less插件
		.pipe(
			autoprefixer({
				cascade: false
			})
		)
		.pipe(cleancss())
		.pipe(
			rename({
				suffix: ".min"
			})
		)
		.pipe(gulp.dest("./dist/src/css"))
		.pipe(
			reload({
				stream: true
			})
		).pipe(connect.reload());
        done()
});

// es6转es5 生产环境打包再放开
gulp.task("js", (done) => {
	var files = [
		{
			name: "home.js",
			url: "./src/js/home.js"
		},
		{
			name: "product.js",
			url: "./src/js/product.js"
		},
		{
			name: "parkScenery.js",
			url: "./src/js/parkScenery.js"
		},
		{
			name: "dynamic.js",
			url: "./src/js/dynamic.js"
		},
		{
			name: "dynamicInfo.js",
			url: "./src/js/dynamicInfo.js"
		},
		{
			name: "visitor.js",
			url: "./src/js/visitor.js"
		},
		{
			name: "aboutUs.js",
			url: "./src/js/aboutUs.js"
		},
		{
			name: "visitorLayout.js",
			url: "./src/js/visitorLayout.js"
		}
	];

	let task = files.map(function (entry) {
		return browserify({ entries: entry.url })
			.transform(babelify, {
				presets: [
					"env" //转换es6代码
				]
			})
			.bundle()
			.pipe(source(entry.name))
			.pipe(buffer())
			.pipe(uglify())
			.pipe(gulp.dest("./dist/src/js"))
			.pipe(
				reload({
					stream: true
				})
			).pipe(connect.reload());
	})

	return es.merge.apply(null, task).on('end',done);
});

// gulp.task("js", () => {
// 	gulp
// 		.src("./src/js/*.js")
// 		.pipe(plumber())
// 		.pipe(uglify()) // 丑化js代码
// 		.pipe(gulp.dest("./dist/src/js"))
// 		.pipe(
// 			reload({
// 				stream: true
// 			})
// 		);
// });

gulp.task("image", (done) => {
	gulp.src("./src/images/**/*").pipe(gulp.dest("./dist/src/images")).pipe(connect.reload());
     done();
});

// gulp.task("index", (done) => {
// 	gulp
// 		.src("./index.html")
// 		.pipe(gulp.dest("./dist"))
// 		.pipe(
// 			reload({
// 				stream: true
// 			})
// 		).pipe(connect.reload());
//          done();
// });

gulp.task("html", (done) => {
	gulp
		.src("./*.{html,htm}")
		.pipe(gulp.dest("./dist"))
		.pipe(
			reload({
				stream: true
			})
		).pipe(connect.reload());
         done();
});

gulp.task("layout", (done) => {
	gulp
		.src("./layout/*.{html,htm}")
		.pipe(gulp.dest("./dist/layout"))
		.pipe(
			reload({
				stream: true
			})
		).pipe(connect.reload());
         done();
});

gulp.task("lib", (done) => {
	gulp
		.src("./src/lib/**/**/**/**/**/*")
		.pipe(gulp.dest("./dist/src/lib"))
		.pipe(
			reload({
				stream: true
			})
		).pipe(connect.reload());
         done();
});

gulp.task("delete", () => {
	return del(["./dist"]); // 加return 方法变为同步
});
gulp.task('watchs',function(){//监听变化执行任务
    //当匹配任务变化才执行相应任务
    watch('./layout/*.{html,htm}',gulp.series('layout'));
	watch("./src/sass/*.{scss,sass}",gulp.series("sass"));
	watch("./src/js/*.js",gulp.series("js"));
	watch("./*.{html,htm}",gulp.series("html"));
	watch("./src/images/**/*",gulp.series("image"));
	watch("./src/lib/**/**/**/**/**/*",gulp.series("lib"));

})
//初始生成app/dist目录
gulp.task('init', gulp.series('delete', gulp.parallel("sass", "js", "image", "layout", "html", "lib")));

gulp.task('connect:dist', function (cb) {
	browserSync.init({
		server: {
			baseDir: "./dist",
			index: "./index.html"
		},
		livereload: true,
		port: 8008
		// middleware: [
		// 	proxy("/api", {
		// 		target: "http://192.168.10.76:9907",
		// 		changeOrigin: true, //是否跨域
		// 		pathRewrite: {
		// 			"^/api": ""
		// 		}
		// 	})
		// ]
	});
	cb()//执行回调，表示这个异步任务已经完成，起通作用,这样会执行下个任务
})
gulp.task('build', gulp.series('init'));


gulp.task(
	"serve",
	gulp.series("build",'connect:dist','watchs','delete',gulp.parallel("sass", "js", "image", "layout", "html", "lib")),
);
